do "E:\yungyu\lottery_baby\do\set_environment.do"

clear
set more off

global control1 "i.pers_gender i.houhld_resid_cd pers_income_pre1 pers_asset_pre1 pers_earnings_pre1 pers_labor_pre1 married_pre1"
global control2 "totbaby_pre1"
global control3 "pre*redeem*"

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
	
drop if prize10M > 5

gen married_presingle = married if married_pre1 == 0
gen divorced_premarried = married == 0 if married_pre1 == 1

replace norm_year = norm_year + 3

cap rm "$table/temp/Tab8.txt"
cap rm "$table/temp/Tab8.xls"
	
foreach y in married_presingle divorced_premarried {
	
	sum `y' if current == 0 & norm == 2
	loc m0 = r(mean)
	sum `y' if current == 0 & norm == 9
	loc m1 = r(mean)
	loc bt = `m1' - `m0'
	loc bt: dis %9.3f `bt'
		
	sum tot_asset if current == 0 & norm == 2 & `y' != .
	loc m0t = r(mean)
	sum tot_asset if current == 0 & norm == 9 & `y' != .
	loc m1t = r(mean)
	loc bt_wt = `m1t' - `m0t' //Baseline trend in Total wealth
	loc m0t: dis %9.3f `m0t'
	loc bt_wt: dis %9.3f `bt_wt'
	
	reghdfe `y' currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3, a(norm#current year) cl(houseid)
	outreg2 using "$table/temp/Tab8.xls" , append bd(3) sd(3) nocon keep(currentxprize*) ctitle(`y',Col6)
	
	forv i = 1(1)2{
		
		sum `y' if current == 0 & norm == 2 & pers_gender == `i'
		loc m0 = r(mean)
		sum `y' if current == 0 & norm == 9 & pers_gender == `i'
		loc m1 = r(mean)
		loc bt = `m1' - `m0'
		loc bt: dis %9.3f `bt'
			
		sum tot_asset if current == 0 & norm == 2 & `y' != . & pers_gender == `i'
		loc m0t = r(mean)
		sum tot_asset if current == 0 & norm == 9 & `y' != . & pers_gender == `i'
		loc m1t = r(mean)
		loc bt_wt = `m1t' - `m0t' //Baseline trend in Total wealth
		loc m0t: dis %9.3f `m0t'
		loc bt_wt: dis %9.3f `bt_wt'
		
		reghdfe `y' currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3 if pers_gender == `i', a(norm#current year) cl(houseid)
		outreg2 using "$table/temp/Tab8.xls" , append bd(3) sd(3) nocon keep(currentxprize*) ctitle(`y',Gender=`i')		
	}
}

import delimited using "$table/temp/Tab8.txt", clear
export excel "$table/Tables.xlsx", sheet("Tab8") sheetreplace 
